package com.jdz.controller.backend;

import com.jdz.common.ServerResponse;
import com.jdz.dao.GroupPrivilegeMapper;
import com.jdz.dao.GroupUserMapper;
import com.jdz.dao.PrivilegeMapper;
import com.jdz.pojo.User;
import com.jdz.service.IRaceTypeService;
import com.jdz.service.IUserService;
import com.jdz.vo.ExportExcelVo;
import com.jdz.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by sun
 **/
@Controller
@RequestMapping("/manage/user")
public class UserManageController {

    @Autowired
    IRaceTypeService iRaceTypeService;
    @Autowired
    GroupUserMapper groupUserMapper;
    @Autowired
    IUserService iUserService;
    @Autowired
    GroupPrivilegeMapper groupPrivilegeMapper;
    @Autowired
    PrivilegeMapper privilegeMapper;

    /**
     * 管理员修改用户密码
     * @param request
     * @param session
     * @param stunoList
     * @param password
     * @return
     */
    @RequestMapping(value = "reset_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse resetAdminPassword(HttpServletRequest request, HttpSession session, @RequestParam(value = "stunoList[]") List<Integer> stunoList, String password) {
        return iUserService.resetAdminPassword(stunoList, password);
    }

    /**
     * 管理员锁定用户账号
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "adminLockUser.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse adminLockUser(HttpServletRequest request, HttpSession session, @RequestParam(value = "stunoList[]") List<Integer> stunoList){
        return iUserService.adminLockUser(stunoList);
    }

    /**
     * 管理员解锁用户账号
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "adminDelockUser.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse adminDeblockUser(HttpServletRequest request, HttpSession session, @RequestParam(value = "stunoList[]") List<Integer> stunoList){
        return iUserService.adminDeblockUser(stunoList);
    }

    /**
     * 管理员删除用户账号
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "deleteUser.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteUser(HttpServletRequest request, HttpSession session, @RequestParam(value = "stunoList[]") List<Integer> stunoList){
        return iUserService.deleteUser(stunoList);
    }

//    /**
//     * 列出所有用户（分页）
//     * @param request
//     * @param session
//     * @return
//     */
//    @RequestMapping(value = "listUser.do",method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse listUser(HttpServletRequest request, HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
//                                   @RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
//        UserVo userVo = (UserVo) session.getAttribute(Const.CURRENT_USER);
//        String url = request.getRequestURL().toString();
//        if (userVo == null) {
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "管理员未登录,请登录");
//        }
//        //校验一下是否是管理员
//        if (iUserService.checkAdminRole(userVo).isSuccess()) {
//            //是管理员
//            if (iUserService.checkPrivilege(userVo, url).isSuccess()) {
//                //增加我们处理逻辑
//                return iUserService.listUserPage(pageNum,pageSize);
//            } else {
//                return ServerResponse.createByErrorMessage("无权限操作,该管理员无此权限");
//            }
//        } else {
//            return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
//        }
//    }

    /**
     * 关键词搜索用户（搜索用户名字）合并列出所有用户的接口
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "search_user.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse searchUser(@RequestParam(value = "name",required = false)
            String name,@RequestParam(value = "groupno",required = false) Integer groupno ,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "5")int pageSize
            , @RequestParam(value = "time_sort",defaultValue = "0") int time_sort) {
        return iUserService.searchUser(name,groupno,pageNum,pageSize,time_sort);
    }

    /**
     * 导出用户信息exel表
     * @param request
     * @param response
     * @param session
     * @param stunoList
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "export_busInfo.do",method = RequestMethod.GET)
    @ResponseBody
    public byte[] exportBusInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session ,@RequestParam(value = "stunoList[]",required = false) List<Integer> stunoList) throws Exception {

        ExportExcelVo<byte[]> exportExcelVo = iUserService.exportBusInfo(stunoList,request,response);
        if(exportExcelVo.getFileName()==null||exportExcelVo.getServerResponse()==null){
            return null;
        }

        response.setHeader("Content-Disposition", "attachment;filename="+exportExcelVo.getFileName());
        ServerResponse<byte[]> serverResponse = exportExcelVo.getServerResponse();
        return serverResponse.getData();

    }



    /**
     * 更新用户信息
     * @param session
     * @param user
     * @return
     */
    @RequestMapping(value = "update_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> update_information(HttpSession session, HttpServletRequest request, User user){

        if(user == null){
            return ServerResponse.createByErrorMessage("管理员未输入修改内容");
        }

        if(user.getStuno() == 0 || user.getStuno() == null){
            return ServerResponse.createByErrorMessage("管理员未输入需要修改的用户id");
        }

        ServerResponse<UserVo> response = iUserService.updateInformation(user);
        return response;
    }

    /**
     * 添加管理员账号
     * @param user
     * @return
     */
    @RequestMapping(value = "AddAdmin.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse AddAdmin(User user){
        return iUserService.AddAdmin(user);
    }
}